package kitwee.com.kanpad.bean;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


import com.blankj.utilcode.util.LogUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import kitwee.com.kanpad.R;

/**
 * Created by sven on 2016/11/9.
 */

public class KanpadRecyleAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    private Context context;// 上下文.
    private List<TaskListBean.RowsBean> listBean = new ArrayList<>();// 数据集合
    private LayoutInflater mLayoutInflater;

    public KanpadRecyleAdapter(Context context, List<TaskListBean.RowsBean> mDatass) {
        mLayoutInflater = LayoutInflater.from(context);
        this.context = context;
        this.listBean = mDatass;
    }

    //定义枚举
    public static enum ITEM_TYPE {
        ITEM_TYPE_IMAGE, ITEM_TYPE_TEXT
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if (viewType == ITEM_TYPE.ITEM_TYPE_IMAGE.ordinal()) {
//            return new MyHolder(mLayoutInflater.inflate(R.layout.recycle_item_layout, parent, false));
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycle_item_layout, parent, false);
            view.setBackgroundResource(R.color.white);
            MyHolder holder = new MyHolder(view);

            return holder;
        } else {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycle_item_layout, parent, false);
            view.setBackgroundResource(R.color.list_title_back_gray);
            MyHolder holder = new MyHolder(view);
            return holder;
        }
    }

    //绑定数据
    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MyHolder && listBean.size() > 0) {

            ((MyHolder) holder).tvNumber.setText(""+position + 1 + "");
            ((MyHolder) holder).tvDeviceName.setText(""+listBean.get(position).getDevice_name() );
            ((MyHolder) holder).tvClassNameValue.setText(""+listBean.get(position).getClass_name_value() + "");
            ((MyHolder) holder).tvProductNumber.setText(""+listBean.get(position).getProduct_number() + "");
            ((MyHolder) holder).tvOrderNumber.setText(""+listBean.get(position).getOrder_number() + "");
            ((MyHolder) holder).tvOrderProduction.setText(""+(int)Math.round(listBean.get(position).getOrder_production()) + "");
            ((MyHolder) holder).tvProductUnits.setText(""+listBean.get(position).getProduct_units() + "");
            ((MyHolder) holder).tvTaskName.setText(""+listBean.get(position).getTask_name() + "");
            ((MyHolder) holder).tvStandardCapacity.setText(""+(int)Math.round(listBean.get(position).getStandard_capacity()) + "");
            ((MyHolder) holder).leaderName.setText(""+listBean.get(position).getLeader_name() + "");
            if(listBean.get(position).getTask_status_value()!=null){
                ((MyHolder) holder).taskStatusText.setText(""+listBean.get(position).getTask_status_value() + "");
            }

            ((MyHolder) holder).tvProductNumber.setText(""+listBean.get(position).getProduct_number() + "");
            setScheduled((MyHolder) holder, position);

            setActual((MyHolder) holder, position);

            if(listBean.get(position).getTask_status_key()!=null){
                setStatusBack((MyHolder) holder, listBean.get(position).getTask_status_key());
            }


            if(listBean.get(position).getTask_status_remarks()==null||"".equals(listBean.get(position).getTask_status_remarks())){
                ((MyHolder) holder).tvRemarks.setText("无");
            }else{
                ((MyHolder) holder).tvRemarks.setText("任务异常:"+listBean.get(position).getTask_status_remarks()+"");
            }

            if (mOnItemClickLitener != null) {
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int pos = holder.getLayoutPosition();
                        mOnItemClickLitener.onItemClick(holder.itemView, pos);
                    }
                });

                holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        int pos = holder.getLayoutPosition();
                        mOnItemClickLitener.onItemLongClick(holder.itemView, pos);
                        return false;
                    }
                });
            }else {
                LogUtils.e("点击事件为null : ");
            }

        }


    }

    private void setActual(MyHolder holder, int position) {
        double actual_production = listBean.get(position).getActual_production();
        holder.tvCtualProduction.setText((int)Math.round(actual_production) + "");
        if(listBean.get(position).getActual_start_time_text()==null){
            holder.tvCtualStartTime.setText("未开始");
        }else {
            holder.tvCtualStartTime.setText(listBean.get(position).getActual_start_time_text().toString());
        }
        if(listBean.get(position).getActual_end_time_text()==null){
            if(listBean.get(position).getActual_start_time_text()==null){
                holder.tvCtualEndTime.setText("未开始");
            }else{
                holder.tvCtualEndTime.setText("未完成");
            }

        }else{
            holder.tvCtualEndTime.setText(listBean.get(position).getActual_end_time_text() + "");
        }

    }

    private void setScheduled(MyHolder holder, int position) {
        holder.tvScheduledProduction.setText((int)Math.round(listBean.get(position).getScheduled_production()) + "");
        holder.tvScheduledStartTime.setText(listBean.get(position).getScheduled_start_time_text() + "");
        holder.tvScheduledEndTime.setText(listBean.get(position).getScheduled_end_time_text() + "");
    }

    private void setStatusBack(MyHolder holder, String task_status_key) {
        if (TextUtils.equals("not_started", task_status_key)) {//未开始
            holder.taskStatusText.setBackgroundResource(R.color.not_start);
        } else if (TextUtils.equals("pause", task_status_key)) {
            holder.taskStatusText.setBackgroundResource(R.color.pause);
        } else if (TextUtils.equals("stop", task_status_key)) {
            holder.taskStatusText.setBackgroundResource(R.color.stop);
        } else if (TextUtils.equals("in_process", task_status_key)) {
            holder.taskStatusText.setBackgroundResource(R.color.in_process);
        } else if (TextUtils.equals("debug_machine", task_status_key)) {
            holder.taskStatusText.setBackgroundResource(R.color.debug_machine);
        } else if (TextUtils.equals("complete", task_status_key)) {
            holder.taskStatusText.setBackgroundResource(R.color.complete);
        } else if (TextUtils.equals("started", task_status_key)) {
            holder.taskStatusText.setBackgroundResource(R.color.started);
        }
    }

    @Override
    public int getItemCount() {

        return listBean == null ? 0 : listBean.size();
    }

    public static class MyHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_number)
        TextView tvNumber;
        @BindView(R.id.tv_device_name)
        TextView tvDeviceName;
        @BindView(R.id.tv_class_name_value)
        TextView tvClassNameValue;
        @BindView(R.id.tv_product_number)
        TextView tvProductNumber;
        @BindView(R.id.tv_order_number)
        TextView tvOrderNumber;
        @BindView(R.id.tv_order_production)
        TextView tvOrderProduction;
        @BindView(R.id.tv_product_units)
        TextView tvProductUnits;
        @BindView(R.id.tv_task_name)
        TextView tvTaskName;
        @BindView(R.id.tv_standard_capacity)
        TextView tvStandardCapacity;
        @BindView(R.id.leader_name)
        TextView leaderName;
        @BindView(R.id.task_status_text)
        TextView taskStatusText;
        @BindView(R.id.tv_remarks)
        TextView tvRemarks;
        @BindView(R.id.tv_scheduled_production)
        TextView tvScheduledProduction;
        @BindView(R.id.tv_scheduled_start_time)
        TextView tvScheduledStartTime;
        @BindView(R.id.tv_scheduled_end_time)
        TextView tvScheduledEndTime;
        @BindView(R.id.tv_ctual_production)
        TextView tvCtualProduction;
        @BindView(R.id.tv_ctual_start_time)
        TextView tvCtualStartTime;
        @BindView(R.id.tv_ctual_end_time)
        TextView tvCtualEndTime;
        public MyHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    @Override
    public int getItemViewType(int position) {

        return position % 2 == 0 ? ITEM_TYPE.ITEM_TYPE_IMAGE.ordinal() : ITEM_TYPE.ITEM_TYPE_TEXT.ordinal();
    }

    public interface OnItemClickLitener {
        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);
    }

    private OnItemClickLitener mOnItemClickLitener;

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

}
